#!/bin/bash

#usage:bash routes_change.sh add|delete (offcampus)

if [ -z "$(id | grep "uid=0")" ];then
	echo "You're not the root!"
	exit 1
fi

get_gateway() 
{	 
	#./backend/get_gateway "$(route -n | grep ^0.0.0.0)"
	echo -n "$2" 
}

UNIT="$(sed -n "/unit/"p ../config/extra_options | grep -o [0-9])"


if [ "$1" = "add" ];then
{	
	GATEWAY=$(get_gateway $(route -n | grep ^0.0.0.0))

	if [ -z "$GATEWAY" ];then
		GATEWAY=$(get_gateway $(route -n | grep ^172.16.0.0))
	fi

	if [ -z "$GATEWAY" ];then
		echo "I can't get your gateway"! >../log/connect_result
		exit 1
	fi

	if [ "$2" = offcampus ];then
		OFF_CAMPUS_BRAS_IP=$(sed -n "2p" ../config/bras_ip)
		ROUTES_ADDED="$(route -n | grep $OFF_CAMPUS_BRAS_IP)"
		
		if [ -n "$ROUTES_ADDED" ];then
			route del default
			route add -net 0.0.0.0 netmask 0.0.0.0 dev ppp$UNIT
			exit 1
		fi	
		
		route del default
		route add -net $OFF_CAMPUS_BRAS_IP netmask 255.255.255.255 gw $GATEWAY
		route add -net 0.0.0.0 netmask 0.0.0.0 dev ppp$UNIT
		exit 0
	fi

	ROUTES_ADDED="$(route -n | grep 172.16.0.0)"

	if [ -n "$ROUTES_ADDED" ];then
		route del default
		route add -net 0.0.0.0 netmask 0.0.0.0 dev ppp$UNIT 

		exit 1
	fi	

	route del default
	route add -net 114.212.0.0 netmask 255.255.0.0 gw $GATEWAY
	route add -net 202.119.32.0 netmask 255.255.224.0 gw $GATEWAY
	route add -net 202.38.2.0 netmask 255.255.255.0 gw $GATEWAY
	route add -net 202.38.3.0 netmask 255.255.255.0 gw $GATEWAY
	route add -net 202.38.126.160 netmask 255.255.255.240 gw $GATEWAY
	route add -net 210.29.240.0 netmask 255.255.240.0 gw $GATEWAY
	route add -net 210.28.128.0 netmask 255.255.240.0 gw $GATEWAY
	route add -net 219.219.112.0 netmask 255.255.240.0 gw $GATEWAY
	route add -net 202.127.247.0 netmask 255.255.255.0 gw $GATEWAY
	route add -net 58.192.48.0 netmask 255.255.248.0 gw $GATEWAY
	route add -net 58.192.32.0 netmask 255.255.240.0 gw $GATEWAY
	route add -net 172.16.0.0 netmask 255.240.0.0 gw $GATEWAY
	route add -net 192.168.0.0 netmask 255.255.0.0 gw $GATEWAY
	route add -net 0.0.0.0 netmask 0.0.0.0 dev ppp$UNIT
}>&/dev/null
elif [ "$1" = "delete" ];then
{
	if [ "$2" = offcampus ];then
		OFF_CAMPUS_BRAS_IP=$(sed -n "2p" ../config/bras_ip)
		ROUTES_ADDED="$(route -n | grep $OFF_CAMPUS_BRAS_IP)"
		if [ -z "$ROUTES_ADDED" ];then
			exit 1
		fi	
		
		GATEWAY=$(get_gateway $ROUTES_ADDED) 
		route del default
		route del -net $OFF_CAMPUS_BRAS_IP netmask 255.255.255.255 gw $GATEWAY
		route add -net 0.0.0.0 netmask 0.0.0.0 gw $GATEWAY
		exit 0
	fi

	ROUTES_ADDED="$(route -n | grep ^172.16.0.0)"

	if [ -z "$ROUTES_ADDED" ];then
		exit 1
	fi	
	
	GATEWAY=$(get_gateway $ROUTES_ADDED) 
	#echo $GATEWAY
	route del -net 114.212.0.0 netmask 255.255.0.0 gw $GATEWAY
	route del -net 202.119.32.0 netmask 255.255.224.0 gw $GATEWAY
	route del -net 202.38.2.0 netmask 255.255.255.0 gw $GATEWAY
	route del -net 202.38.3.0 netmask 255.255.255.0 gw $GATEWAY
	route del -net 202.38.126.160 netmask 255.255.255.240 gw $GATEWAY
	route del -net 210.29.240.0 netmask 255.255.240.0 gw $GATEWAY
	route del -net 210.28.128.0 netmask 255.255.240.0 gw $GATEWAY
	route del -net 219.219.112.0 netmask 255.255.240.0 gw $GATEWAY
	route del -net 202.127.247.0 netmask 255.255.255.0 gw $GATEWAY
	route del -net 58.192.48.0 netmask 255.255.248.0 gw $GATEWAY
	route del -net 58.192.32.0 netmask 255.255.240.0 gw $GATEWAY
	route del -net 172.16.0.0 netmask 255.240.0.0 gw $GATEWAY
	route del -net 192.168.0.0 netmask 255.255.0.0 gw $GATEWAY
	route add -net 0.0.0.0 netmask 0.0.0.0 gw $GATEWAY 
}&>/dev/null
fi

exit 0
